package cn.darkhorse.ticket.basis.service.impl;

import cn.darkhorse.ticket.basis.mapper.TicketCustomerInfoMapper;
import cn.darkhorse.ticket.basis.pojo.TicketCustomerInfo;
import cn.darkhorse.ticket.basis.service.TicketCustomerInfoService;
import cn.darkhorse.ticket.code.RepeatCheck;
import cn.darkhorse.ticket.constants.TicketConstants;
import cn.darkhorse.ticket.production.pojo.TicketProductionPlanInfo;
import cn.darkhorse.ticket.production.service.TicketProductionPlanInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 客户信息 服务实现类
 * </p>
 *
 * @author maoguanjun
 * @since 2024-01-10
 */
@Service
public class TicketCustomerInfoServiceImpl extends ServiceImpl<TicketCustomerInfoMapper, TicketCustomerInfo> implements TicketCustomerInfoService {
    @Resource
    private RepeatCheck<TicketCustomerInfo> repeatCheck;
    @Resource
    private TicketProductionPlanInfoService ticketProductionPlanInfoService;
    /**
     * 列表
     *
     * @param dataName
     * @param dataCode
     * @return
     */
    @Override
    public List<TicketCustomerInfo> listAll(String dataName, String dataCode) {
        LambdaQueryWrapper<TicketCustomerInfo> wrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dataName)){
            wrapper.like(TicketCustomerInfo::getName,dataName);
        }
        if(StringUtils.isNotBlank(dataCode)){
            wrapper.like(TicketCustomerInfo::getCode,dataCode);
        }
        wrapper.orderByDesc(TicketCustomerInfo::getGmtCreate);
        return list(wrapper);
    }

    /**
     * 查看
     *
     * @param dataId
     * @return
     */
    @Override
    public TicketCustomerInfo view(Long dataId) {
        return getById(dataId);
    }

    /**
     * 保存
     *
     * @param info
     * @return
     */
    @Override
    public boolean saveData(TicketCustomerInfo info) {
        //TODO 生成编码
        repeatCheck.repeat(this,info,TicketConstants.BUSINESS_TYPE_KH);
        LambdaQueryWrapper<TicketCustomerInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(TicketCustomerInfo::getName,info.getName());
        if(null!=info.getId()){
            if(isQuote(info.getId())){
                throw new ServiceException("该客户信息已被生产计划或工单引用，无法修改!");
            }
            wrapper.ne(TicketCustomerInfo::getId,info.getId());
        }
        long count = count(wrapper);
        if(count>0){
            throw new ServiceException("该客户名称系统已存在！");
        }
        return saveOrUpdate(info);
    }

    /**
     * 删除
     *
     * @param dataId
     * @return
     */
    @Override
    public boolean delete(Long dataId) {
        //TODO 删除校验
        if(isQuote(dataId)){
            throw new ServiceException("该客户信息已被生产计划或工单引用，无法修改!");
        }
        return removeById(dataId);
    }

    /**
     * 是否被引用
     *
     * @param dataId
     * @return
     */
    @Override
    public boolean isQuote(Long dataId) {
        //生产计划
        LambdaQueryWrapper<TicketProductionPlanInfo> planWrapper=new LambdaQueryWrapper<>();
        planWrapper.eq(TicketProductionPlanInfo::getCustomerId,dataId);
        long planCount = ticketProductionPlanInfoService.count(planWrapper);
        //工单
        if(planCount>0){
            return true;
        }

        return false;
    }
}
